package ar.fiuba.tp7552.buildadmin.webactions.expensas;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Properties;

import org.apache.fop.apps.Driver;
import org.apache.fop.apps.XSLTInputHandler;

import ar.fiuba.tp7552.buildadmin.model.expensas.Liquidacion;
import ar.fiuba.tp7552.buildadmin.pdf.PDFCreator;
import ar.fiuba.tp7552.buildadmin.service.ExpensaService;
import ar.fiuba.tp7552.buildadmin.webactions.BaseAction;

import com.opensymphony.xwork2.Preparable;

public class LiquidacionAction extends PDFCreator implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ExpensaService expensaService;
	private Liquidacion liquidacion;
	
	// ~~ Constructor ====================================================
	
	public LiquidacionAction(ExpensaService expensaService) {
		super();
		this.expensaService = expensaService;
	}
	
	@Override
	public void prepare() throws Exception {
		if (liquidacion != null && liquidacion.getId() != null) {
			liquidacion = expensaService.getLiquidacion(liquidacion.getId());
		}
	}
	
	public String load() {
		return BaseAction.SUCCESS;
	}
	
	public String crearCertificado() {
    	crearPdf();
		return BaseAction.SUCCESS;
	}

	// ~~ getters y setters ========================================================
	public Liquidacion getLiquidacion() {
		return liquidacion;
	}

	public void setLiquidacion(Liquidacion liquidacion) {
		this.liquidacion = liquidacion;
	}

	// ~~ pdfcreator  ========================================================
	public InputStream getInputStream(){
		try{
			return new FileInputStream(new File("certificado.pdf"));
		}catch(Exception e){}
		return null;
   }

    public void crearPdf(){
		String xslParam = "certificado.xsl";
		generarPdf(xslParam);

        return;
	 }
	
	 /***************************************************************************
	 * Renderea al pdf a partir de un xml
	 **************************************************************************/
   @SuppressWarnings("deprecation")
   public byte[] renderXML(XSLTInputHandler input)  throws Exception {
       ByteArrayOutputStream out = new ByteArrayOutputStream();

       Driver driver = new Driver();
       driver.setLogger(null);
       driver.setRenderer(Driver.RENDER_PDF);
       driver.setOutputStream(out);
       try {
           driver.render(input.getParser(), input.getInputSource());                       
       } catch (Exception e) {           
          e.printStackTrace();
       }
       byte[] oPdfBinario = out.toByteArray();
       return oPdfBinario;
   }
   
   public String getXmlAsString() {
       String xsl = "expensa.xsl";
       StringBuffer xml = new StringBuffer("");
       DecimalFormat df = new DecimalFormat("###########0.00");

		Properties prop = new Properties();
		try {
			prop.load(this.getClass().getResourceAsStream("/app.properties"));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	            
    	String pathImg = prop.getProperty("pathPDF") + "img/";

       xml.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
       xml.append("<"+xsl+">");

       //IMAGENES
       xml.append("<signature>" + pathImg + "signature.jpg" + "</signature>");

       //Expensa
       xml.append("<expensa>");
       		xml.append("<nombreEdificio>" + liquidacion.getPropiedad().getTipo().getEdificio().getNombre() + "</nombreEdificio>");
       		xml.append("<uf>" + liquidacion.getPropiedad().getUF() + "</uf>");
       		xml.append("<tipoDetalle>" + liquidacion.getTipo() + "</tipoDetalle>");
       		xml.append("<liquidacionNro>" + liquidacion.getNumero() + "</liquidacionNro>");
       		xml.append("<tipo>" + liquidacion.getTipo() + "</tipo>");
       		xml.append("<monto>" + df.format(liquidacion.getMonto()) + "</monto>");
       		xml.append("<intereses>" + df.format(liquidacion.getInteres()) + "</intereses>");
       		xml.append("<deuda>" + df.format(liquidacion.getDeudaPrevia()) + "</deuda>");
       		SimpleDateFormat date = new SimpleDateFormat("MMMM 'de' yyyy");
       		xml.append("<periodo>" + date.format(liquidacion.getPeriodo()) + "</periodo>");

       		SimpleDateFormat fulldf = new SimpleDateFormat("dd/MM/yyyy");
       		xml.append("<vencimiento>" + fulldf.format(liquidacion.getVencimiento1()) + "</vencimiento>");
       		if (liquidacion.getVencimiento2() != null){
	       		xml.append("<vencimiento2>" + fulldf.format(liquidacion.getVencimiento2()) + "</vencimiento2>");
	       		xml.append("<intereses2>" + df.format(liquidacion.getInteres2Vto()) + "</intereses2>");
       		}
       xml.append("</expensa>");

       xml.append("</"+xsl+">");
       return xml.toString();
   }   

}
